package week3;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;

/**
 * @author The Setting sun
 * @create 2021-09-23 15:24
 */
public class work3 {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
//        frame.setLayout(new GridLayout(2, 1));

        JPanel panel1 = new JPanel();

        JPanel p1 = new JPanel();
        JLabel label11 = new JLabel("R");
        label11.setFont(new Font("微软雅黑", Font.BOLD, 16));
        JSlider slider1 = new JSlider(0, 255);
        JLabel label12 = new JLabel();
        label12.setFont(new Font("微软雅黑", Font.BOLD, 16));

        p1.add(label11);
        p1.add(slider1);
        p1.add(label12);

        JPanel p2 = new JPanel();
        JLabel label21 = new JLabel("G");
        label21.setFont(new Font("微软雅黑", Font.BOLD, 16));
        JSlider slider2 = new JSlider(0, 255);
        JLabel label22 = new JLabel();
        label22.setFont(new Font("微软雅黑", Font.BOLD, 16));

        p2.add(label21);
        p2.add(slider2);
        p2.add(label22);

        JPanel p3 = new JPanel();
        JLabel label31 = new JLabel("B");
        label31.setFont(new Font("微软雅黑", Font.BOLD, 16));
        JSlider slider3 = new JSlider(0, 255);
        JLabel label32 = new JLabel();
        label32.setFont(new Font("微软雅黑", Font.BOLD, 16));

        p3.add(label31);
        p3.add(slider3);
        p3.add(label32);

        JPanel panel2 = new JPanel();
        JTextArea area = new JTextArea(10, 15);
        area.setEditable(false);
        area.setBackground(Color.PINK);

        ChangeListener changeListener = new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                int r = slider1.getValue();
                int g = slider2.getValue();
                int b = slider3.getValue();

                area.setBackground(new Color(r, g, b));
                label12.setText(r + "");
                label22.setText(g + "");
                label32.setText(b + "");
            }
        };

        slider1.addChangeListener(changeListener);
        slider2.addChangeListener(changeListener);
        slider3.addChangeListener(changeListener);

        panel1.add(p1);
        panel1.add(p2);
        panel1.add(p3);
        panel2.add(area);

        frame.add(panel1, BorderLayout.CENTER);
        frame.add(panel2, BorderLayout.EAST);

        frame.setSize(500, 400);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
